package org.thirdHero.db.service;

import com.github.pagehelper.PageHelper;
import org.thirdHero.db.dao.SysRoleMapper;
import org.thirdHero.db.domain.SysRole;
import org.thirdHero.db.domain.SysRoleExample;
import org.thirdHero.db.vo.SysRoleVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysRoleService {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleService.class);
    @Resource
    private SysRoleMapper sysRoleMapper;


    private final SysRole.Column[] result = new SysRole.Column[]{
            SysRole.Column.id, SysRole.Column.num, SysRole.Column.pid, SysRole.Column.name, SysRole.Column.deptid, SysRole.Column.tips, SysRole.Column.version
    };
    public List<SysRoleVo> querySelective(String name, Integer page, Integer limit, String sort, String order){
        PageHelper.startPage(page,limit);
        List<SysRoleVo> roles =this.sysRoleMapper.queryRoleMessage(name);
        return roles;
    }
    public int countSelective(String name,Integer page,Integer limit,String sort,String order){
        SysRoleExample example = commonSelective(name);
        return (int) sysRoleMapper.countByExample(example);
    }
    private SysRoleExample commonSelective(String name) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        return example;
    }
    public SysRole queryRoleByName(String name){
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        return this.sysRoleMapper.selectOneByExample(example);
    }
    public List<SysRole> queryByRoles(List<Integer> ids){
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return sysRoleMapper.selectByExample(example);
    }
    public int updateRole(SysRole role){
        return this.sysRoleMapper.updateByPrimaryKey(role);
    }
    public SysRole queryRoleById(Integer id){
        return this.sysRoleMapper.selectByPrimaryKey(id);
    }
    public int saveRole(SysRole role) {
        return this.sysRoleMapper.insert(role);
    }

    public int deleteRoleById(Integer id) {
        return this.sysRoleMapper.deleteByPrimaryKey(id);
    }
}
